package com.explorer.ider.bootguide.adapter;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.explorer.ider.bootguide.R;

import java.util.List;

/**
 * Created by Eric on 2016/9/9.
 */
public class MyWifiListAdapter extends BaseAdapter{

    Context mContext;
    private List<String> mWifiNameList;//传入的wifi名称
    private List<WifiInfo> mWifiInfoList;//传入wifi信息类
    private LayoutInflater inflate;//布局填充
    private int[] wifiWaftList;//wifi信号
    private boolean[] wifiIfPassWordList;//wifi是否需要密码

    public MyWifiListAdapter(Context context,List<String> mWifiNameList,int[] wifiWaftList,boolean[] wifiIfPassWordList){
        this.mContext = context;
        this.mWifiNameList = mWifiNameList;
        this.wifiWaftList = wifiWaftList;
        this.wifiIfPassWordList = wifiIfPassWordList;
        inflate = LayoutInflater.from(context);
    }

    public void refreshWifiList(List<String> mWifiNameList,int[] wifiWaftList,boolean[] wifiIfPassWordList){
        notifyDataSetInvalidated();
        this.mWifiNameList = mWifiNameList;
        this.wifiWaftList = wifiWaftList;
        this.wifiIfPassWordList = wifiIfPassWordList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mWifiNameList.size();
    }//获得长度，返回列表项的长度，传入数据的size

    @Override
    public Object getItem(int i) {
        return mWifiNameList.get(i);
    }//获得数据的其中一项数据

    @Override
    public long getItemId(int i) {
        return i;
    }//获得其中一项的id，直接返回i

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {//填充没一项数据，根据getCount的长度，循环执行该方法，i是position
        ViewHolder holder;//缓存view的一个类
        if (view == null){//如果这个View没加载过，将他存入ViewHolder
            view = inflate.inflate(R.layout.wifi_list_adapter_item,null);
            holder = new ViewHolder();
            holder.wifiWaft = (ImageView) view.findViewById(R.id.id_wifi_list_item_waft_image);
            holder.wifiIfPassword = (ImageView) view.findViewById(R.id.id_wifi_list_item_if_password_image);
            holder.wifiName = (TextView) view.findViewById(R.id.id_wifi_list_item_name);
            view.setTag(holder);
        }else{//否则读取这个View
            holder = (ViewHolder) view.getTag();
        }
        //将Viewholder中的数据加载到视图上
        if (wifiWaftList[i] < 100) {
            holder.wifiWaft.setImageResource(R.mipmap.mipmap_wifi_level1);//wifi信号
        }else if (wifiWaftList[i] < 135){
            holder.wifiWaft.setImageResource(R.mipmap.mipmap_wifi_level12);//wifi信号
        }else if (wifiWaftList[i] >= 135){
            holder.wifiWaft.setImageResource(R.mipmap.mipmap_wifi_level3);//wifi信号
        }
        holder.wifiIfPassword.setImageResource(!wifiIfPassWordList[i] ? R.mipmap.mipmap_wifi_lockon : R.mipmap.mipmap_wifi_lockoff);
        holder.wifiName.setText(mWifiNameList.get(i));


        return view;

    }

    public class ViewHolder{
        public ImageView wifiWaft;
        public ImageView wifiIfPassword;
        public TextView wifiName;
    }

}
